package eic.muckup.garden;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.CursorJoiner.Result;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Measurings extends ListActivity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	private int mType;
	private final int MOISTRUE = 0;
	private final int TEMPERATURE = 1;
	private ListView mMoisterListView;
	private ArrayList<MeasuringsDataClass> mMeasureingList;
	private MeasuringsDataClass mMeasureingData;
	
	private final String MIN_MOISTURE_VALUE = "minMoisture";
	private final String MAX_MOISTURE_VALUE = "maxMoisture";
	private final String MIN_TEMPERATURE_VALUE = "minTemperature";
	private final String MAX_TEMPERATURE_VALUE = "maxTemperature";
	
	private MeasuringAdapter aa;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.measuring_list_view);
		
		mMoisterListView = getListView();
		mMeasureingData = new MeasuringsDataClass();
		TextView textHeadline = (TextView) findViewById(R.id.sensorDataHeadline);
		
		Integer type = getIntent().getIntExtra("DataType", -1);
		switch(type)
		{
		case MOISTRUE:
			mType = MOISTRUE;
			textHeadline.setText("Moistrue");
			break;
		case TEMPERATURE:
			mType = TEMPERATURE;
			textHeadline.setText("Temperature");
			break;
		default:
			Intent intent = new Intent();
			setResult(RESULT_OK, intent);
			finish();
		}

		mMeasureingList = new ArrayList<MeasuringsDataClass>();
		
		setData(mMeasureingList);
		
		aa = new MeasuringAdapter(this, R.layout.measuring_list_row, mMeasureingList);
		
		mMoisterListView.setAdapter(aa);
	}
 
	private void setData(ArrayList<MeasuringsDataClass> list){
		MeasuringsDataClass m = new MeasuringsDataClass();
		m.setMoitureDato("25/05-2012");
		m.setMoistureTime("10:00");
		m.setMoistureValue(2.0F);
		m.setTemperatureDato("25/05-2012");
		m.setTemperatureTime("10:00");
		m.setTemperatureValue(2.0F);
		list.add(m);
		
		MeasuringsDataClass n = new MeasuringsDataClass();
		n.setMoitureDato("25/05-2012");
		n.setMoistureTime("12:00");
		n.setMoistureValue(25.0F);
		n.setTemperatureDato("25/05-2012");
		n.setTemperatureTime("12:00");
		n.setTemperatureValue(25.0F);
		list.add(n);
		
		MeasuringsDataClass v = new MeasuringsDataClass();
		v.setMoitureDato("25/05-2012");
		v.setMoistureTime("14:00");
		v.setMoistureValue(95.0F);
		v.setTemperatureDato("25/05-2012");
		v.setTemperatureTime("14:00");
		v.setTemperatureValue(95.0F);
		list.add(v);
		
		
	}
	
	private class MeasuringAdapter extends ArrayAdapter<MeasuringsDataClass> {

		private ArrayList<MeasuringsDataClass> mItems;
		private Context mContext;
		public MeasuringAdapter(Context context, int textViewResourceId,
				ArrayList<MeasuringsDataClass> items) {
			super(context, textViewResourceId, items);
			mItems = items;
			mContext = context;
		}
		
		 @Override
	        public View getView(int position, View convertView, ViewGroup parent) {
	                View v = convertView;
	                if (v == null) {
	                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	                    v = vi.inflate(R.layout.measuring_list_row, null);
	                }
	                MeasuringsDataClass MDC = mItems.get(position);
	                if (MDC != null) {
	                        TextView dt = (TextView) v.findViewById(R.id.dato_text);
	                        TextView tt = (TextView) v.findViewById(R.id.tid_text);
	                        TextView vt = (TextView) v.findViewById(R.id.value_text);
	                   if(mType == MOISTRUE){
	                	   if (dt != null) {
	                        	dt.setText("Dato: " + MDC.getMoitureDato());
	                        }	
	                        
	                        if (tt != null) {
	                        	tt.setText("Time: " + MDC.getMoistureTime());
	                        }
	                        	
	                        if(vt != null){	
	                        	vt.setText("Moistrue: " + MDC.getMoistureValue() + "%");
	                        	vt.setTextColor(moistrueColor(MDC.getMoistureValue()));
	                        }
	                	}else{
	                		 if (dt != null) {
		                        	dt.setText("Dato: " + MDC.getTemperatureDato());
		                        }	
		                        
		                        if (tt != null) {
		                        	tt.setText("Time: " + MDC.getTemperatureTime());
		                        }
		                        	
		                        if(vt != null){	
		                        	vt.setText("Temperature: " + MDC.getTemperatureValue() + "C");
		                        	vt.setTextColor(moistrueColor(MDC.getTemperatureValue()));
		                        }
	                	}
	                }
	                return v;
	        }
		 
		 public int moistrueColor(Float data){
			 SharedPreferences prefs = getSharedPreferences("LIMIT_VALUE", Activity.MODE_PRIVATE);
         	 
         	 if(data < prefs.getFloat(MIN_MOISTURE_VALUE, 0)){
         		 return Color.MAGENTA;
         	 }
         	 else if(data > prefs.getFloat(MAX_MOISTURE_VALUE, 100)){
         		 return Color.BLUE;
         	 }
         	 else{
         		 return Color.BLACK;
         	 }
		 }
		 
		 public int temperatureColor(Float data){
			 SharedPreferences prefs = getSharedPreferences("LIMIT_VALUE", Activity.MODE_PRIVATE);
         	 
         	 if(data < prefs.getFloat(MIN_TEMPERATURE_VALUE, -20)){
         		 return Color.BLUE;
         	 }
         	 else if(data > prefs.getFloat(MAX_TEMPERATURE_VALUE, 100)){
         		 return Color.RED;
         	 }
         	 else{
         		 return Color.BLACK;
         	 }
		 }
	}
}
